package com.tuzhi.common.helper;

import com.tuzhi.common.constants.ResultConstant;
import com.tuzhi.common.domain.ResultBO;

public class ResultHelper implements ResultConstant {

    public static <E> ResultBO<E> success() {
        return build(SUCCESS);
    }

    public static <E> ResultBO<E> success(String msg) {
        return new ResultBO(SUCCESS.getCode(), msg);
    }

    public static <E> ResultBO<E> success(String msg, Object data) {
        return new ResultBO(SUCCESS.getCode(), msg, data);
    }

    public static <E> ResultBO<E> error() {
        return build(ERROR);
    }

    public static <E> ResultBO<E> error(String msg) {
        return new ResultBO(ERROR.getCode(), msg);
    }

    public static <E> ResultBO<E> error(String code, String msg) {
        return new ResultBO(code, msg);
    }

    public static <E> ResultBO<E> error(String msg, E data) {
        return new ResultBO(ERROR.getCode(), msg, data);
    }


    public static <E> ResultBO<E> build(ResultConstant.RC rc) {
        return new ResultBO(rc.getCode(), rc.getMsg());
    }

    public static <T extends ResultBO> T setMsg(T t, String msg) {
        t.setMsg(msg);
        return t;
    }

    public static <T extends ResultBO> T setCodeMsg(T t, String code, String msg) {
        t.setMsg(msg);
        return t;
    }

    public static <T extends ResultBO> T setMsgAndData(T t, String msg, Object data) {
        t.setMsg(msg);
        t.setData(data);
        return t;
    }

    public static boolean isSuccess(ResultBO resultBO) {
        if (resultBO != null) {
            return ResultConstant.SUCCESS.getCode().equals(resultBO.getCode());
        }
        return false;
    }

}
